import axios from 'axios';

axios.interceptors.response.use(function (response) {
    const resp = response.data;
    if (resp && resp.code === 0) {
        return Promise.resolve(resp);
    } else {
        alert(resp.message);
        return Promise.reject(resp.message);
    }
    return response;
}, function (error) {
    console.log("=============API CALL ERROR============");
    console.log(error);
    return Promise.reject(error);
})

function get(url) {
    return axios.get(url);
}

function post(url, data) {
    return axios.post(url, data);
}

export default {
    chat: `/chat`,
    getDataSources: function() {
        return get('/db-metadata');
    },
    getDatabase: function(dataSource) {
        dataSource = encodeURIComponent(dataSource)
        return get(`/db-metadata/${dataSource}`);
    },
    getSchema: function(dataSource, database) {
        dataSource = encodeURIComponent(dataSource);
        database = encodeURIComponent(database)
        return get(`/db-metadata/${dataSource}/${database}`);
    },
    getTable: function(dataSource, database, schema) {
        dataSource = encodeURIComponent(dataSource);
        database = encodeURIComponent(database);
        schema = encodeURIComponent(database);
        return get(`/db-metadata/${dataSource}/${database}/${schema}`);
    }
}